#include "captureCallBack.h"
#include "DeckLinkAPI.h"

#include <iostream>
#include <fstream>
#include <sstream>
#include <string.h>


using namespace std;

void CaptureCallback::dPrint(string data)
{

	string output="[CaptureCallback] " + data;

	//cout << output << endl;
};


CaptureCallback::CaptureCallback()
{
	dPrint("INIT");
	once=false;
	count=0;
	//outputFile.open("test.avi",fstream::in | fstream::out | fstream::binary | fstream::app );
};



HRESULT CaptureCallback::VideoInputFrameArrived(IDeckLinkVideoInputFrame* videoFrame, IDeckLinkAudioInputPacket* audioFrame)
{
	if (!once)
	{

		/*	
		count++;
		if (count==200)
		{
			once=true;
			dPrint("DONE");
		}
		*/
	
		int length=videoFrame->GetRowBytes()*videoFrame->GetHeight();
		
		/*	
		string s;
		stringstream out; 
		out << length; //1843200
		//out << strlen(frameBytes); //1920000
		//out << videoFrame->GetRowBytes(); //2560
		s = out.str();
		dPrint(s);
		*/
	
		char *frameBytes;
		videoFrame->GetBytes((void **)&frameBytes);

		char *buffer=new char[length];
		strncpy(buffer,frameBytes,length);
		
		cout << buffer ;
		//outputFile.write(frameBytes,length);
		delete buffer;
		
	}/*
	else
	{
		outputFile.close();
	}*/
};


ULONG CaptureCallback::AddRef(void)
{
	return 0;
};


ULONG CaptureCallback::Release(void)
{
	return 0;
};


HRESULT CaptureCallback::VideoInputFormatChanged(BMDVideoInputFormatChangedEvents, IDeckLinkDisplayMode*, BMDDetectedVideoInputFormatFlags)
{
	return S_OK;
};
	
